package com.ddtj.demo1.service.impl;

import com.ddtj.demo1.dao.UserJpa;
import com.ddtj.demo1.entity.User;
import com.ddtj.demo1.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Optional;
@Service
public class UserService implements IUserService {

    @Autowired
    UserJpa userJpa;

    @Override
    public boolean isExist(Integer id, String username) {
        User user = new User();
        user.setUsername(username);
        Example<User> userExample = Example.of(user);
        Optional<User> one = userJpa.findOne(userExample);
        if(!one.isPresent()){
            return false;
        }
        if(one.isPresent() && one.get().getId().equals(id)){
            return false;
        }
        return true;
    }

    @Override
    public Page<User> findPage(Pageable pageable, User user) {
        Example<User> userExample = Example.of(user);
        Page<User> page = userJpa.findAll(userExample, pageable);
        return page;
    }

}
